package com.hex.liyue.launchdatemodule.presenter;

import android.text.TextUtils;

import com.hex.liyue.LiYueApplication;
import com.hex.liyue.commonmodule.listener.UploadFileListener;
import com.hex.liyue.commonmodule.manager.HttpManager;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.net.ResponseListener;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.OssUtils;
import com.hex.liyue.commonmodule.utils.SPUtil;
import com.hex.liyue.launchdatemodule.activity.LaunchListActivity;
import com.hex.liyue.launchdatemodule.bean.LaunchDateBean;

/**
 * 这是一个发起约会业务类
 * Created by WJ on 2016/6/16.
 */
public class LaunchDataPresenter {
    private ILaunchDataView iLaunchDataView;

    public LaunchDataPresenter(ILaunchDataView iLaunchDataView) {
        this.iLaunchDataView = iLaunchDataView;
    }

    public void finish(final String gift_id, final String gps_x, final String gps_y) {
        //想要做什么
        if (TextUtils.isEmpty(iLaunchDataView.getEvent())) {
            iLaunchDataView.showT("请告诉我你想要做什么");
            return;
        }
        //地点
        if (TextUtils.isEmpty(iLaunchDataView.getLocation())) {
            iLaunchDataView.showT("地点不能为空");
            return;
        }
        //时间
        if (TextUtils.isEmpty(iLaunchDataView.getInvitationsTime())) {
            iLaunchDataView.showT("时间不能为空");
            return;
        }
        //时间不能大于当前时间
        if (DateUtils.getDateLongTime("yyyy-MM-dd HH:mm", iLaunchDataView.getInvitationsTime()) < System.currentTimeMillis()) {
            iLaunchDataView.showT("时间必须大于当前时间");
            return;
        }

        //视频
        if (iLaunchDataView.getVideo() != null && iLaunchDataView.getVideo().exists()) {
            iLaunchDataView.showLod("请求中...");
            String key = OssUtils.getInstance().objectKeyVideo + UserManager.getUserId() + "_" + iLaunchDataView.getVideo().getName();//KEy=前缀+文件名
            OssUtils.getInstance().uploadfile(key, iLaunchDataView.getVideo().getPath(), new UploadFileListener() {
                @Override
                public void onSucess(String path) {
                    upload(path,gift_id,gps_x,gps_y);
                }

                @Override
                public void onFailure() {
                    L.d("WJ", "视频上传失败");
                }

                @Override
                public void onProgress(long progress) {

                }
            });
        } else {
            iLaunchDataView.showDialog();
        }


    }

    public void upload(String path,String gift_id,String gps_x,String gps_y) {
        HttpManager.getInstance().createAppointment(
                UserManager.getUserId(),
                iLaunchDataView.getEvent(),
                iLaunchDataView.getLocation(),
                String.valueOf(DateUtils.getDateLongTime("yyyy-MM-dd HH:mm", iLaunchDataView.getInvitationsTime()) / 1000),
                path,
                iLaunchDataView.getRemark(),
                gift_id,
                gps_x,
                gps_y,
                new ResponseListener<LaunchDateBean>() {
                    @Override
                    public void onSuccess(LaunchDateBean launchDateBean) {
                        iLaunchDataView.showT("发起成功");
                        iLaunchDataView.closeLod();
                        SPUtil.getInt(LiYueApplication.getInstance(),"appointmentId",Integer.parseInt(launchDateBean.data.appointment_id));
                        LaunchListActivity.appointmentId=launchDateBean.data.appointment_id;
                        iLaunchDataView.startLaunchListActivity();
                    }

                    @Override
                    public void onFailure(String responseresult) {
                        iLaunchDataView.showT(responseresult);
                        iLaunchDataView.closeLod();
                    }
                }
        );
    }
}
